﻿@using PagedList.Mvc;

@model PagedList.IPagedList<Style>

@section styles{
    @Styles.Render("~/Content/PagedList.css")
}

@functions {
    private string SortOrderLink(string displayName, string name, string search, int pageSize, bool isTrash, int productId)
    {
        string url = Url.Action("Index", new
        {
            sortOrder = ViewData[name + "Sort"],
            search = search,
            pageSize = pageSize,
            isTrash = isTrash,
            productId = productId
        });

        string icon = String.IsNullOrWhiteSpace(ViewData[name + "Icon"] as String)
            ? DataTableConstant.DefaultSortIcon
            : ViewData[name + "Icon"] as String;

        return "<a href='" + url + "'>" + displayName + icon + "</a>";
    }
}

@{
    var strHp = new StringHelper();
    
    string title = ViewBag.Title;
    string search = ViewBag.Search;
    string sortOrder = ViewBag.SortOrder;
    int pageSize = ViewBag.PageSize;
    bool isTrash = ViewBag.IsTrash;
    int trashCount = ViewBag.TrashCount;
    int listCount = ViewBag.ListCount;
    string returnListUrl = ViewBag.ReturnListUrl;
    SelectList pageSizeDDL = ViewBag.PageSizeDDL;
    Product product = ViewBag.Product;
    List<PropertyType> propertyTypes = ViewBag.PropertyTypes;

    int index = Model.FirstItemOnPage;
    string controller = "style";

    ViewBag.Title = "Danh sách " + title;
}

<section class="content-header">
    <h1>
        @title.ToUpper() <small>@(!isTrash ? "danh sách" : "thùng rác")</small>
    </h1>
    <ol class="breadcrumb">
        <li>
            <a href="@Url.Action("Index", "Admin" )">
                <i class="fa fa-home"></i> Trang chủ
            </a>
        </li>
        <li>
            <a href="@Url.Action("Index", "Product")">
                Danh sách sản phẩm
            </a>
        </li>
        <li>
            <a href="@Url.Action("Index", "Product" , new { search = product.Code })">
                @product.Name - @product.Code
            </a>
        </li>
        <li class="active">@(!isTrash ? "Danh sách" : "Thùng rác") @title</li>
    </ol>
</section>

<section class="content">

    @Html.Action("Alert", "Layout", new { partialView = "_AlertHandling" })

    <div class="box box-primary">
        <div class="box-body">
            @using (Html.BeginForm("Index", "Style", FormMethod.Get))
            {
                @Html.Hidden("IsTrash", isTrash)

                @Html.DropDownList("PageSize", pageSizeDDL,
                        new
                        {
                            @class = "form-control auto-width inline",
                            onchange = "this.form.submit()"
                        })

                if (!isTrash)
                {
                    bool roleCreate = User.IsInRole("Admin") || User.IsInRole("Image Create");

                    if (roleCreate)
                    {
                        <a href="@Url.Action("Create", new { productId = product.Id, returnListUrl = returnListUrl })" class="btn btn-warning">
                            <i class="fa fa-plus"></i> Tạo mới
                        </a>
                    }
                    <a href="@Url.Action("Index", new { productId = product.Id, isTrash = true })" class="btn btn-danger @(trashCount > 0 ? "" : "disabled")">
                        <i class="fa fa-trash-o"></i> Thùng rác (@trashCount)
                    </a>
                }
                else
                {
                    <a href="@Url.Action("Index", new { productId = product.Id })" class="btn btn-success">
                        <i class="fa fa-file-text"></i> Danh sách (@listCount)
                    </a>
                }
            }
            <br />
            <div class="table-responsive">
                <table class="table table-bordered table-hover">
                    <thead>
                        <tr>
                            <th>#</th>
                            @foreach (var propType in propertyTypes)
                            {
                                <th>
                                    @propType.Name
                                </th>
                            }
                            <th>
                                @Html.Raw(SortOrderLink("Đơn giá", "UnitPrice", search, pageSize, isTrash, product.Id))
                            </th>
                            <th>
                                @Html.Raw(SortOrderLink("Số lượng", "Quantity", search, pageSize, isTrash, product.Id))
                            </th>
                            <th>
                                @Html.Raw(SortOrderLink("Tạo mới", "DateCreated", search, pageSize, isTrash, product.Id))
                            </th>
                            <th>
                                @Html.Raw(SortOrderLink("Chỉnh sửa", "DateModified", search, pageSize, isTrash, product.Id))
                            </th>
                            <th class="text-center">
                                <a id="btnRefresh" href="@Request.Url.AbsoluteUri" class="btn btn-success btn-sm hidden">
                                    <i class="fa fa-refresh"></i> Làm mới
                                </a>
                            </th>
                        </tr>
                    </thead>
                    <tbody>
                        @foreach (Style item in Model)
                        {
                            bool roleEdit = User.IsInRole("Admin") || User.IsInRole("Style Edit Public")
                                || (User.IsInRole("Style Edit Private") && item.CreatedBy == User.Identity.Name);

                            bool roleRecycle = User.IsInRole("Admin") || User.IsInRole("Style Recycle Public")
                                || (User.IsInRole("Style Recycle Private") && item.CreatedBy == User.Identity.Name);

                            bool roleDelete = User.IsInRole("Admin") || User.IsInRole("Style Delete Public")
                                || (User.IsInRole("Style Delete Private") && item.CreatedBy == User.Identity.Name);
                            
                            <tr>
                                <td>
                                    @(index++)
                                </td>
                                @foreach (PropertyType propType in propertyTypes)
                                {
                                    var tmp = item.StyleProperties.FirstOrDefault(x => x.Property.PropertyTypeId == propType.Id);
                                    <td>
                                        @if (tmp != null)
                                        {
                                            @tmp.Property.Name
                                        }
                                    </td>
                                }
                                <td class="text-right">
                                    <a class="no-href"
                                       data-id="@item.Id"
                                       data-toggle="modal"
                                       data-target="#detailsModal"
                                       title="Chi tiết"
                                       onclick="Details(this, '@controller')">
                                        @strHp.PriceFormat(item.UnitPrice, "usd")
                                    </a>
                                </td>
                                <td class="text-right">
                                    @item.Quantity
                                </td>
                                <td>
                                    <div>
                                        @item.DateCreated.ToString(DataTableConstant.DateTimeFormat)
                                    </div>
                                    <div>
                                        @item.CreatedBy
                                    </div>
                                </td>
                                <td>
                                    @if (item.DateModified != null)
                                    {
                                        <div>
                                            @(((DateTime)item.DateModified).ToString(DataTableConstant.DateTimeFormat))
                                        </div>
                                        <div>
                                            @item.ModifiedBy
                                        </div>
                                    }
                                </td>
                                <td class="control">
                                    @if (!isTrash)
                                    {
                                        if (roleEdit)
                                        {
                                            <a href="@Url.Action("Edit", new { id = item.Id, returnListUrl = returnListUrl })"
                                               data-toggle="tooltip" data-placement="top" title="Chỉnh sửa">
                                                <i class="fa fa-edit"></i>
                                            </a>
                                        }
                                        if (roleRecycle)
                                        {
                                            <a class="no-href" data-id="@item.Id" data-toggle="tooltip" data-placement="top"
                                               title="Xóa tạm thời" onclick="Recycle(this, '@controller', 0)">
                                                <i class="fa fa-trash-o"></i>
                                            </a>
                                        }
                                    }
                                    else
                                    {
                                        if (roleRecycle)
                                        {
                                            <a class="no-href" data-id="@item.Id" data-toggle="tooltip" data-placement="top"
                                               title="Khôi phục" onclick="Recycle(this, '@controller', 1)">
                                                <i class="fa fa-repeat"></i>
                                            </a>
                                        }
                                        if (roleDelete)
                                        {
                                            <a class="no-href" data-id="@item.Id" data-toggle="modal" data-target="#deleteModal"
                                               title="Xóa vĩnh viễn" onclick="Details(this, '@controller')">
                                                <i class="fa fa-times"></i>
                                            </a>
                                        }
                                    }
                                </td>
                            </tr>
                        }
                    </tbody>
                </table>
            </div>
            <br />
            <div class="row">
                <div class="col-md-6">
                    <div class="well-sm">
                        Hiển thị <span class="badge">@Model.FirstItemOnPage</span>
                        đến <span class="badge">@Model.LastItemOnPage</span>
                        trong <span class="badge">@Model.TotalItemCount</span> @title
                    </div>
                </div>
                <div class="col-md-6 text-right">
                    @Html.PagedListPager(Model, page => Url.Action("Index", new
                    {
                        page,
                        sortOrder = sortOrder,
                        search = search,
                        pageSize = pageSize,
                        isTrash = isTrash,
                        productId = product.Id
                    }), new PagedListRenderOptions
                    {
                        LinkToFirstPageFormat = DataTableConstant.LinkToFirstPageFormat,
                        LinkToPreviousPageFormat = DataTableConstant.LinkToPreviousPageFormat,
                        LinkToNextPageFormat = DataTableConstant.LinkToNextPageFormat,
                        LinkToLastPageFormat = DataTableConstant.LinkToLastPageFormat,
                        MaximumPageNumbersToDisplay = 5
                    })
                </div>
            </div>
        </div>
    </div>
</section>

@Html.Action("ManagerModal", "Layout", new
{
    partialView = "_DetailsModal",
    title = title
})

@Html.Action("ManagerModal", "Layout", new
{
    partialView = "_DeleteModal",
    title = title,
    controller = controller,
    returnListUrl = returnListUrl
})

@section scripts{
    <script>
        ActiveMenu('product');
    </script>
}